<template>
  <div id="app">
    <!-- 登录页面不显示导航栏 -->
    <nav v-if="!isLoginPage" class="navbar">
      <div class="navbar-brand">
        <router-link to="/" class="brand-link">
          法律AI助手
        </router-link>
      </div>
      
      <div class="navbar-menu">
        <router-link to="/" class="nav-item" active-class="active">
          <span class="nav-icon">🏠</span>
          首页
        </router-link>
        <router-link to="/qa" class="nav-item" active-class="active">
          <span class="nav-icon">💬</span>
          智能问答
        </router-link>
        <router-link to="/cases" class="nav-item" active-class="active">
          <span class="nav-icon">⚖️</span>
          案例检索
        </router-link>
        <router-link to="/config" class="nav-item" active-class="active">
          <span class="nav-icon">⚙️</span>
          系统设置
        </router-link>
      </div>
      
      <!-- 用户信息和操作 -->
      <div class="user-menu">
        <div v-if="isLoggedIn" class="user-dropdown">
          <button class="user-button">
            <span class="user-icon">👤</span>
            {{ currentUser }}
          </button>
          <div class="dropdown-menu">
            <router-link to="/profile" class="dropdown-item">
              个人资料
            </router-link>
            <button @click="handleLogout" class="dropdown-item">
              退出登录
            </button>
          </div>
        </div>
        <router-link v-else to="/login" class="login-button">
          登录
        </router-link>
      </div>
    </nav>
    
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script>
import { computed, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useStore } from 'vuex';
import { logout } from '@/services/auth';

export default {
  name: 'App',
  setup() {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    
    // 初始化认证状态
    onMounted(() => {
      store.dispatch('initAuth');
    });
    
    // 计算属性：是否在登录页
    const isLoginPage = computed(() => {
      return route.path === '/login';
    });
    
    // 计算属性：当前用户
    const currentUser = computed(() => {
      return store.getters.currentUser;
    });
    
    // 计算属性：是否已登录
    const isLoggedIn = computed(() => {
      return store.getters.isLoggedIn;
    });
    
    // 退出登录
    const handleLogout = () => {
      logout();
      router.push('/login');
    };
    
    return {
      isLoginPage,
      currentUser,
      isLoggedIn,
      handleLogout
    };
  }
}
</script>

<style>
:root {
  --primary-color: #10a37f;
  --primary-light: rgba(16, 163, 127, 0.1);
  --text-color: #333;
  --light-text-color: #666;
  --border-color: #ddd;
  --background-color: #f7f7f9;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: Arial, sans-serif;
  background-color: var(--background-color);
  color: var(--text-color);
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.navbar {
  background-color: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 0.8rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.navbar-brand {
  font-weight: 700;
  font-size: 1.25rem;
}

.brand-link {
  color: var(--primary-color);
  text-decoration: none;
}

.navbar-menu {
  display: flex;
  gap: 1.5rem;
}

.nav-item {
  color: var(--light-text-color);
  text-decoration: none;
  font-weight: 500;
  display: flex;
  align-items: center;
  padding: 0.5rem 0;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
}

.nav-item:hover {
  color: var(--primary-color);
}

.nav-item.active {
  color: var(--primary-color);
  border-bottom: 2px solid var(--primary-color);
}

.nav-icon {
  margin-right: 6px;
}

.main-content {
  flex: 1;
  padding: 0;
  display: flex;
  flex-direction: column;
  margin-top: 60px; /* 为固定的navbar留出空间 */
}

.user-menu {
  position: relative;
}

.user-dropdown {
  position: relative;
}

.user-button {
  background: none;
  border: none;
  color: var(--light-text-color);
  font-weight: 500;
  cursor: pointer;
  padding: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-icon {
  margin-right: 4px;
}

.user-dropdown:hover .dropdown-menu {
  display: block;
}

.dropdown-menu {
  display: none;
  position: absolute;
  right: 0;
  top: 100%;
  min-width: 160px;
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  padding: 0.5rem 0;
  z-index: 1000;
}

.dropdown-item {
  display: block;
  padding: 0.6rem 1rem;
  color: var(--text-color);
  text-decoration: none;
  font-size: 0.9rem;
  text-align: left;
  background: none;
  border: none;
  width: 100%;
  cursor: pointer;
  transition: background-color 0.2s;
}

.dropdown-item:hover {
  background-color: #f5f5f5;
  color: var(--primary-color);
}

.login-button {
  background-color: var(--primary-color);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  text-decoration: none;
  font-weight: 500;
  font-size: 0.9rem;
  transition: background-color 0.2s;
}

.login-button:hover {
  background-color: #0d926f;
}

@media (max-width: 768px) {
  .navbar {
    flex-wrap: wrap;
    padding: 0.6rem 1rem;
  }
  
  .navbar-menu {
    order: 3;
    flex: 0 0 100%;
    margin-top: 0.8rem;
    justify-content: space-between;
    gap: 0.5rem;
  }
  
  .nav-item {
    font-size: 0.85rem;
    flex-direction: column;
    gap: 0.2rem;
  }
  
  .nav-icon {
    margin-right: 0;
  }
  
  .user-button {
    font-size: 0.9rem;
  }
}
</style> 